<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" label-width="120px">
      <el-form-item label="集团" prop="companyId">
        <el-select v-model="dataForm.companyId" filterable placeholder="集团">
          <el-option v-for="item in dict.companyId" :key="item.code" :label="item.name" :value="parseInt(item.code)"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学校名称" prop="schoolName">
        <el-input v-model="dataForm.schoolName" placeholder="学校名称"></el-input>
      </el-form-item>

      <el-form-item label="省份" prop="type">
        <el-select v-model="dataForm.provinceCode" placeholder="请选省份" @change="selectCity()">
          <el-option label="全国" value="0">全国</el-option>
          <el-option v-for="item in dict.provinceCode" :key="item.code" :label="item.name" :value="item.code"></el-option>
        </el-select>
      </el-form-item>
      <!--
      <el-form-item label="城市" prop="type">
        <el-select v-model="dataForm.cityCode" placeholder="请选城市">
          <el-option v-for="item in dict.cityCode" :key="item.code" :label="item.name" :value="item.code"></el-option>
        </el-select>
      </el-form-item>
      -->
      <el-form-item prop="type" label="城市" v-show="dataForm.provinceCode !== '' && dataForm.provinceCode !== '0'">
        <el-select v-model="dataForm.cityCodeList" multiple placeholder="请选城市">
          <el-option v-for="item in dict.cityCode" :key="item.code" :label="item.name" :value="item.code"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
import { IObject } from "@/types/interface";
export default defineComponent({
  setup() {
    return reactive({
      dict: {
        cityCode: [{ code: "", name: "" }],
        provinceCode: [{ code: "", name: "" }],
        companyId: [{ code: "", name: "" }]
      },
      visible: false,
      dataForm: {
        id: "",
        schoolName: "",
        companyId: "",
        cityCode: "",
        cityCodeList: [] as IObject[],
        provinceCode: ""
      },
      cityCodeListDefault: [] as IObject[],
      peroidCodeListDefault: [] as IObject[],
      sceneCodeListDefault: [] as IObject[],
      cityList: [] as IObject[]
    });
  },
  computed: {
    dataRule() {
      return {};
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        this.cityCodeListDefault = [];
        baseService.urlDict("/fii/tarea/dict/0").then((a) => {
          this.dict.provinceCode = a;
        });
        baseService.urlDict("fii/fiicompany/dict").then((a) => {
          this.dict.companyId = a;
        });
        if (this.dataForm.id) {
          this.getInfo();
          this.selectCity();
        }
      });
    },
    // 获取信息
    getInfo() {
      baseService.get("/fii/fiischool/" + this.dataForm.id).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        //this.dataForm = res.data;
        this.dataForm = {
          ...this.dataForm,
          ...res.data,
          cityCodeList: []
        };

        // 选中的cityCode
        for (let i = 0; i < res.data.cityCodeList.length; i++) {
          if (this.cityList.filter((item: IObject) => item.id === res.data.cityCodeList[i])) {
            this.dataForm.cityCodeList.push(res.data.cityCodeList[i]);
            continue;
          }
          this.cityCodeListDefault.push(res.data.cityCodeList[i]);
        }

        if (this.dataForm.provinceCode) {
          this.querySelectedCity();
        }
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/fii/fiischool", {
          ...this.dataForm,
          cityCodeList: [...this.dataForm.cityCodeList, ...this.cityCodeListDefault]
        }).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    },
    // 获取城市
    selectCity() {
      baseService.urlDict("/fii/tarea/dict/" + this.dataForm.provinceCode).then((a) => {
        this.dict.cityCode = a;
      });
    },
    // 获取城市2
    querySelectedCity() {
      baseService.post("/fii/fiischool/getCityName", { code: this.dataForm.cityCode }).then((res) => {
        this.dict.cityCode = res.data;
      });
    }
  }
});
</script>
